﻿/*
' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using GP_Hosting_ContactForm;
using Recaptcha.Web;
using System.Reflection;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace GP_Hosting_ContactForm
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from GP_Hosting_ContactFormModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
   
    
    public partial class View : GP_Hosting_ContactFormModuleBase, IActionable
    {
       
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (EnableGooglereCaptcha)
                {
                    Recaptcha1.Enabled = true;
                    Recaptcha1.PrivateKey = GetreCaptchaPrivateKey;
                    Recaptcha1.PublicKey = GetreCaptchaPublicKey;
                    switch (GetreCaptchaTheme)
                    {
                        case "Clean":
                            Recaptcha1.Theme = RecaptchaTheme.Clean;
                            break;
                        case "White":
                            Recaptcha1.Theme = RecaptchaTheme.White;
                            break;
                        case "Red":
                            Recaptcha1.Theme = RecaptchaTheme.Red;
                            break;
                        case "Blackglass":
                            Recaptcha1.Theme = RecaptchaTheme.Blackglass;
                            break;
                        default:
                            Recaptcha1.Theme = RecaptchaTheme.Clean;
                            break;
                    }
                }
                else
                {
                    Recaptcha1.Enabled = false;
                    Recaptcha1.Visible = false;
                }

          



            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }


        public List<string> VerifyFields()
        {
          
            List<string> FieldValidation = new List<string>();

            if (ValidateNameField)
            {
                if (string.IsNullOrWhiteSpace(txtname.Text))
                {
                    FieldValidation.Add("false");
                    lblname.Text = NameError;
                }
                }

            if (ValidateSurnameField)
            {
                if (string.IsNullOrWhiteSpace(txtsurname.Text))
                {
                FieldValidation.Add("false");
                    lblsurname.Text = SurnameError;
                }

            }

            if (ValidateContactNumber)
            {
                if (string.IsNullOrWhiteSpace(txtcontactno.Text))
                {
                FieldValidation.Add("false");
                    lblcontact.Text = ContactError;
                }

            }

            if (ValidateEmailField)
            {
                var IsValidEmail = new Regex(RegexEmail);

                if (!IsValidEmail.IsMatch(txtemail.Text))
                {
                FieldValidation.Add("false");
                    lblemail.Text = EmailErrorMSG;
                }

            }
            if (ValidateMessageField)
            {

                if (string.IsNullOrWhiteSpace(txtmessage.Text))
                {
                    FieldValidation.Add("false");
                    lblmessage.Text = MessageError;
                }else if (ProfanityCheck)
                {

                    if (profanityChecker(txtmessage.Text))
                    {
                        FieldValidation.Add("false");
                        lblmessage.Text = ProfanityMSG;
                    }
                    }
                    
            }



            

            if (EnableGooglereCaptcha)
            {

                RecaptchaVerificationResult result =  Recaptcha1.Verify();

                if (!string.IsNullOrWhiteSpace(Recaptcha1.Response))
                {
                    if (result == RecaptchaVerificationResult.Success)
                    {
                       FieldValidation.Add("True");
                    }
                    if (result == RecaptchaVerificationResult.IncorrectCaptchaSolution)
                    {
                        FieldValidation.Add("false");
                        lblcapture.Text = IncorrectCaptcha;
                    }
                }
                else
                {
                    FieldValidation.Add("false");
                    lblcapture.Text = BlankRecaptcha;
                }
                

            }




            return FieldValidation;

        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            EmailObject email = new EmailObject();
            email.name = txtname.Text;
            email.surname = txtsurname.Text;
            email.contactnumber = txtcontactno.Text;
            email.fromaddress = txtemail.Text;
            email.message = txtmessage.Text;
            try
            {
                clearerrors();
               
                
                if (!VerifyFields().Contains("false"))
                {

                    if (!UseDnnSettings)
                    {

                        if (SendEmail(email))
                        {
                            emailsent.CssClass = "emailsent";
                            emailsent.Text = EmailSentOK;
                            if (EnableReplyMail)
                            {
                                ReplyToUser(email);
                            }
                            
                        }
                        else
                        {
                            emailsent.CssClass = "emailnotsent";
                            emailsent.Text = EmailNotSent;
                        }

                    }
                    else
                    {

                        if (SendEmailUsingDnn(txtemail.Text, GetFromAddress, GetToAddress, GetEmailsubject, txtmessage.Text))
                        {
                            emailsent.CssClass = "emailsent";
                            emailsent.Text = EmailSentOK;
                            
                            if (EnableReplyMail)
                            {
                                ReplyToUser(email);
                            }
                        
                        }
                        else
                        {
                            emailsent.CssClass = "emailnotsent";
                            emailsent.Text = EmailNotSent;
                        }



                     
                    }

                }

            }
            catch (Exception ex)
            {
               var name = MethodBase.GetCurrentMethod().Name;

            }

        }

        public void clearerrors()
        {
            lblcapture.Text = string.Empty;
            lblcontact.Text = string.Empty;
            lblemail.Text = string.Empty;
            lblmessage.Text = string.Empty;
            lblmsg.Text = string.Empty;
            lblname.Text = string.Empty;
            lblsurname.Text = string.Empty;
            emailsent.Text = string.Empty;

        }
    }
}